package com.antoco.operation.bigscreen.controller.modelcontroller.impl;

import com.antoco.operation.bigscreen.controller.modelcontroller.intf.AreaPhController;
import com.antoco.operation.bigscreen.model.dto.AreaPhDTO;

import com.antoco.operation.bigscreen.model.entity.AreaPh;
import com.antoco.operation.bigscreen.second.ValidList;
import com.antoco.operation.bigscreen.mapper.AreaPhMapper;
import com.antoco.operation.bigscreen.service.ChannelDataService;
import com.antoco.operation.bigscreen.service.autogeneration.intf.AreaPhService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


@RequestMapping("/model/area-ph")
@RestController
public class AreaPhControllerImpl implements AreaPhController {

    @Autowired
    private AreaPhService areaPhService;

    @Autowired
    private AreaPhMapper areaPhMapper;

    @Autowired
    private ChannelDataService channelDataService;

    @Override
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public AreaPhDTO save(@Validated @RequestBody AreaPhDTO areaPhDTO) {
        AreaPh areaPh = areaPhMapper.asEntity(areaPhDTO);
        areaPhService.save(areaPh);
        channelDataService.pushAllLists(this.getClass().getName());
        return areaPhDTO;
    }

    @Override
    @PostMapping("save-lists")
    @ResponseStatus(HttpStatus.CREATED)
    public List<AreaPhDTO> saveLists(@Validated @RequestBody ValidList<AreaPhDTO> areaPhDTOLists) {
        areaPhService.save(areaPhMapper.asEntityList(areaPhDTOLists));
        //当完成数据的操作后，调用该封装的方法获取变化后的数据进行推送
        channelDataService.pushAllLists(this.getClass().getName());
        return null;
    }

    @Override
    @GetMapping("/{id}")
    public AreaPhDTO findById(@PathVariable("id") String id) {
        AreaPh areaPh = areaPhService.findById(id).orElse(null);
        return areaPhMapper.asDTO(areaPh);
    }

    @Override
    @DeleteMapping("/{id}")
    public void delete(@PathVariable("id") String id) {
        areaPhService.deleteById(id);
        channelDataService.pushAllLists(this.getClass().getName());
    }

    @Override
    @GetMapping
    public List<AreaPhDTO> list() {
        return areaPhMapper.asDTOList(areaPhService.findAll());
    }

    @Override
    @GetMapping("/page-query")
    public Page<AreaPhDTO> pageQuery(Pageable pageable) {
        Page<AreaPh> areaPhPage = areaPhService.findAll(pageable);
        List<AreaPhDTO> dtoList = areaPhPage
                .stream()
                .map(areaPhMapper::asDTO).collect(Collectors.toList());
        return new PageImpl<>(dtoList, pageable, areaPhPage.getTotalElements());
    }

    @Override
    @PutMapping("/{id}")
    public AreaPhDTO update(@RequestBody AreaPhDTO areaPhDTO, @PathVariable("id") String id) {
        AreaPh areaPh = areaPhMapper.asEntity(areaPhDTO);
        areaPhService.update(areaPh, id);
        channelDataService.pushAllLists(this.getClass().getName());
        return areaPhDTO;
    }
}